<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller\management;


use app\admin\model\management\Store as StoreModel;
use app\admin\model\management\Order as OrderModel;
use app\admin\model\management\LeasePrice as LeasePriceModel;
use app\admin\service\TriggerService;
use app\common\constants\AdminConstant;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Request;
use think\facade\Db;
use app\admin\model\ManagementStoreBecome;
/**
 * Class Admin
 * @package app\admin\controller\system
 * @ControllerAnnotation(title="管理员管理")
 */
class Order extends AdminController
{

    use \app\admin\traits\Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
       
        $this->model  =new OrderModel();
    }
    
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model->where($where)->count();
            $list  = $this->model->where($where)->page($page, $limit)->order('create_time desc')->select()->toArray();
            foreach ($list as $k => $v) {
                $members_nickname=Db::name('members')->where('members_id',$v['members_id'])->value('members_nickname');
                $list[$k]['name']=base64_decode($members_nickname);
            }
            $data  = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="添加")
     */
    public function add($id = null)
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                // 'cate_id|cate_id'    => 'require|>:0',
            ];
            $this->validate($post, $rule);
            if($post['spec_type']=="more"){
                $data=$this->getData($post);
                 $post=$data['data'];
                 $post['lease_value']=$data['lease_value'];
            }
           
               Db::startTrans();
            try {
                $save = $this->model->save($post);
                Db::commit();
            } catch (\Exception $e) {
                $this->error($e->getMessage());
                Db::rollback();
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $specs=$this->getSpec();
        $this->assign([
            'id'=>$id,
            'specs'=>$specs
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {   
        $id  = Request::param('id');
        $row = $this->model->where('id',$id)->find();
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
           
            $rule = [];
            $this->validate($post, $rule);
          if($post['spec_type']=="more"){
                $data=$this->getData($post);
            }
            $post=$data['data'];
            $post['lease_value']=$data['lease_value'];
               Db::startTrans();
            try {
                $save = $row->save($post);
                Db::commit();
            } catch (\Exception $e) {
                $this->error($e->getMessage());
                Db::rollback();
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $specs=$this->getSpec();
        $this->assign([
            'row'=>$row,
            'specs'=>$specs
        ]);
       
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {  
    
        $row = $this->model->whereIn('id', $id)->select();
     
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $save = $this->model->whereIn('id', $id)->delete();
       
            foreach ($row as $k => $v) {
                del_image($v['purchase']);
                del_image($v['thumb']);
            }
            $this->lease_price->whereIn('lease_id', $id)->delete();
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

    /**
     * 
     * @NodeAnotation(title="属性修改")
     */
    public function modify()
    {
        $post = $this->request->post();
     
        $rule = [
            // 'cate_id|ID'    => 'require',
            'field|字段' => 'require',
            'value|值'  => 'require',
        ];
        $this->validate($post, $rule);
        // if (!in_array($post['field'], $this->allowModifyFields)) {
        //     $this->error('该字段不允许修改：' . $post['field']);
        // }
     
        $row = $this->model->where('id',$post['id'])->find();
        empty($row) && $this->error('数据不存在');
        try {
            $row->save([
                $post['field'] => $post['value'],
            ]);
        } catch (\Exception $e) {
            // $this->error($e->getMessage());
            $this->success('保存成功');
        }
        $this->success('保存成功');
    }
 
   public function details(){
        $id=input('id');
         $this->assign([
            'id'=>$id
        ]);
        return $this->fetch();
   }
   public function getOrdersDetail(){
        $post = $this->request->get();
        $page=$post['page'];
        $limit=$post['limit'];
        $where['order_id']=$post['order_id'];
           $count = Db::name('order_server')->where($where)->count();
           $list  = Db::name('order_server')->where($where)->select()->toArray();
           foreach ($list as $k => $v) {
              if($v['type']==1){
                $list[$k]['type_name']="门票";
                $list[$k]['server_name']=Db::name('ticket')->where('id',$v['server_id'])->value('title');
              }elseif($v['type']==2){
                $list[$k]['type_name']="租赁";
                $list[$k]['server_name']=Db::name('lease')->where('id',$v['server_id'])->value('title');
                $time_interval=Db::name('spec_item')->where('id',$v['time_id'])->value('item');
                $list[$k]['house']=$v['time_id']?$v['house_title']."【".$time_interval."】":"";
              }
           }
        $data  = [
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list,
        ];
        return json($data);
    }
}
